﻿Imports System.Data.SqlClient
Public Class Frm_HoaDon
    Dim clsdm As New Cls_DanhMuc
    Private Sub Frm_HoaDon_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
        'clsdm.FillDataGirdView("select * from CTHoaDon a,SanPham b where a.masp=b.masp and mahd='" & Me.txtmahd.Text.Trim & "'", Me.dtg, Me.dts)
        an()

    End Sub
#Region " Thêm,xóa,sửa...."
    Private Sub btthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthem.Click

        If Me.btthem.Text = "&Thêm (F2)" Then
            Me.btthem.Text = "&Hủy"
            Me.btluu.Enabled = True
            Me.btsua.Enabled = False
            Me.btxoa.Enabled = False
            Me.cbxtennv.Enabled = True
            Me.cbxtennv.Focus()
            Me.txttenkh.Enabled = True
            clsdm.FillC1ComBo(Me.cbxtennv, "Select * from V_NhanVien", "TenNV", "MaNV")
            clsdm.FillC1ComBo(Me.cbxtensp, "Select masp,tensp,dongia from SANPHAM", "TenSP", "MaSP")
            clsdm.FillDataText(Me.txtmahd, "Tang_MaHd")
            clsdm.FillDataGirdView("select * from V_HoaDon where mahd='" & Me.txtmahd.Text.Trim & "'", Me.dtg, Me.dts)
        Else
            Me.btthem.Text = "&Thêm (F2)"
            Me.txtmahd.Text = ""
           
            'an()
        End If
        Me.txttongsl.Text = 0
        Me.txttongtien.Text = 0
    End Sub

    Private Sub btluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btluu.Click
        If Me.btluu.Text = "&Thêm Hóa Đơn (F3)" Then
            Me.btluu.Text = "&Cập Nhật Chi Tiết (F3)"
            Dim cmd As New SqlCommand("ThemHD", con)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@Mahd", SqlDbType.NVarChar).Value = txtmahd.Text.Trim
            cmd.Parameters.Add("@Manv", SqlDbType.Int).Value = cbxtennv.SelectedValue
            cmd.Parameters.Add("@Tenkh", SqlDbType.NVarChar).Value = txttenkh.Text.Trim
            cmd.Parameters.Add("@Ngaylap", SqlDbType.DateTime).Value = dtp_ngay.Text
            cmd.Parameters.Add("@Ghichu", SqlDbType.NVarChar).Value = txtghichu.Text.Trim
            cmd.Parameters.Add("@TongSL", SqlDbType.Int).Value = 0
            cmd.Parameters.Add("@TongTien", SqlDbType.Money).Value = 0
            Try
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox("Lỗi Cập Nhật " & ex.Message, MsgBoxStyle.Critical, "Thông Báo")
            End Try
            Me.btthem.Text = "&Thêm (F2)"
            Me.cbxtensp.Focus()
            Mo()
        Else
            Me.btluu.Text = "&Cập Nhật Chi Tiết (F3)"
            Dim mycmd As New SqlCommand("ThemCTHD", con)
            mycmd.CommandType = CommandType.StoredProcedure
            mycmd.Parameters.Add("@mahd", SqlDbType.NVarChar).Value = txtmahd.Text
            mycmd.Parameters.Add("@Masp", SqlDbType.NVarChar).Value = cbxtensp.SelectedValue
            mycmd.Parameters.Add("@Soluong", SqlDbType.Int).Value = soluong.Value.ToString
            mycmd.Parameters.Add("@DVT", SqlDbType.NVarChar).Value = cbxdvt.Text
            mycmd.Parameters.Add("@Dongia", SqlDbType.Money).Value = CInt(txtdongia.Text)
            mycmd.Parameters.Add("@Thanhtien", SqlDbType.Money).Value = CInt(txtthanhtien.Text)
            Try
                mycmd.ExecuteNonQuery()

                clsdm.FillDataText(Me.txttongsl, "select TongSl from HoaDon where mahd='" & Me.txtmahd.Text.Trim & "'")
                clsdm.FillDataText(Me.txttongtien, "select TongTien from HoaDon where mahd='" & Me.txtmahd.Text.Trim & "'")
                Me.txtsoluong.Text = 0
                Me.cbxtensp.Focus()
            Catch ex As Exception
                MsgBox("Lỗi Cập Nhật " & ex.Message, MsgBoxStyle.Critical, "Thông Báo")
            End Try
            clsdm.FillDataGirdView("select * from V_HoaDon where mahd='" & Me.txtmahd.Text.Trim & "'", Me.dtg, Me.dts)
        End If
    End Sub
    Private Sub btxoa_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btxoa.Click
        Dim strma, strten As String
        strma = dtg.CurrentRow.Cells(0).Value.ToString()
        strten = dtg.CurrentRow.Cells(1).Value.ToString()
        If MsgBox("Bạn có chắc xóa : " & strma & "Có tên: " & strten, MsgBoxStyle.YesNo, "Thông Báo") = MsgBoxResult.Yes Then
            Try
                Dim myCmd As New SqlCommand("XoaCTHD", con)
                myCmd.CommandType = CommandType.StoredProcedure
                myCmd.Parameters.Add("@mahd", SqlDbType.NVarChar).Value = Me.txtmahd.Text
                myCmd.Parameters.Add("@masp", SqlDbType.NVarChar).Value = Me.cbxtensp.SelectedValue
                myCmd.ExecuteNonQuery()
                clsdm.FillDataText(Me.txttongsl, "select TongSl from HoaDon where mahd='" & Me.txtmahd.Text.Trim & "'")
                clsdm.FillDataText(Me.txttongtien, "select TongTien from HoaDon where mahd='" & Me.txtmahd.Text.Trim & "'")
            Catch ex As Exception
                MsgBox("Lỗi khi xóa" & ex.Message)
                Exit Sub
            End Try
        End If
        clsdm.FillDataGirdView("select * from V_HoaDon where mahd='" & Me.txtmahd.Text.Trim & "'", Me.dtg, Me.dts)
    End Sub
    Private Sub btsua_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btsua.Click
        Dim mycmd As New SqlCommand("SuaCTHD", con)
        mycmd.CommandType = CommandType.StoredProcedure
        mycmd.Parameters.Add("@mahd", SqlDbType.NVarChar).Value = txtmahd.Text
        mycmd.Parameters.Add("@Masp", SqlDbType.NVarChar).Value = cbxtensp.SelectedValue
        mycmd.Parameters.Add("@Soluong", SqlDbType.Int).Value = txtsoluong.Text
        mycmd.Parameters.Add("@DVT", SqlDbType.NVarChar).Value = cbxdvt.Text
        mycmd.Parameters.Add("@Dongia", SqlDbType.Money).Value = CInt(txtdongia.Text)
        mycmd.Parameters.Add("@Thanhtien", SqlDbType.Money).Value = CInt(txtthanhtien.Text)
        Try
            mycmd.ExecuteNonQuery()
            clsdm.FillDataText(Me.txttongsl, "select TongSl from HoaDon where mahd='" & Me.txtmahd.Text.Trim & "'")
            clsdm.FillDataText(Me.txttongtien, "select TongTien from HoaDon where mahd='" & Me.txtmahd.Text.Trim & "'")
        Catch ex As Exception
            MsgBox("Lỗi Cập Nhật " & ex.Message, MsgBoxStyle.Critical, "Thông Báo")
        End Try
        clsdm.FillDataGirdView("select * from V_HoaDon where mahd='" & Me.txtmahd.Text.Trim & "'", Me.dtg, Me.dts)
    End Sub
    Private Sub btthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthoat.Click
        Me.Close()
    End Sub
#End Region

   

    Private Sub cbxdvt_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxdvt.GotFocus
        clsdm.FillCombo(Me.cbxdvt, "select * from V_DonViTinh", "TenDVT", "Madvt")
    End Sub
    Private Sub cbxtensp_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxtensp.SelectedValueChanged
        clsdm.FillDataText(Me.txtdongia, "select dongia from SanPham where masp='" & Me.cbxtensp.SelectedValue & "'")
        clsdm.FillCombo(Me.cbxdvt, "select * from SanPham a,donvitinh b where a.madvt=b.madvt and tensp='" & Me.cbxtensp.Text & "'", "Tendvt", "Madvt")
    End Sub
    Private Sub txttongtien_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txttongtien.TextChanged
        If IsNumeric(Me.txttongtien.Text.Trim) = True And Len(Me.txttongtien.Text) > 3 Then
            Me.txttongtien.Text = Format(CInt(Me.txttongtien.Text), "#,###")
        End If
    End Sub
    Private Sub txtsoluong_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtsoluong.TextChanged
        If Me.txtsoluong.Text <> "" And IsNumeric(Me.txtsoluong.Text) Then
            Me.txtthanhtien.Text = Me.txtdongia.Text * Me.txtsoluong.Text
        End If

    End Sub

    Private Sub txtdongia_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtdongia.TextChanged
        If IsNumeric(Me.txtdongia.Text.Trim) = True And Len(Me.txtdongia.Text) > 3 Then
            Me.txtdongia.Text = Format(CInt(Me.txtdongia.Text), "#,###")
        End If
    End Sub
    Private Sub txtthanhtien_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtthanhtien.GotFocus
        If Me.txtsoluong.Text <> "" And IsNumeric(Me.txtsoluong.Text) Then
            Me.txtthanhtien.Text = Me.txtdongia.Text * Me.txtsoluong.Text
        End If
        If IsNumeric(Me.txtthanhtien.Text.Trim) = True And Len(Me.txtthanhtien.Text) > 3 Then
            Me.txtthanhtien.Text = Format(CInt(Me.txtthanhtien.Text), "#,###")
        End If
    End Sub

    Private Sub txtthanhtien_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtthanhtien.TextChanged
        If IsNumeric(Me.txtthanhtien.Text.Trim) = True And Len(Me.txtthanhtien.Text) > 3 Then
            Me.txtthanhtien.Text = Format(CInt(Me.txtthanhtien.Text), "#,###")
        End If
    End Sub
    Sub an()
        Me.cbxtensp.Enabled = False
        Me.txtsoluong.Enabled = False
        Me.cbxdvt.Enabled = False
        Me.txtdongia.Enabled = False
        Me.txtthanhtien.Enabled = False
        Me.cbxtennv.Enabled = False
        Me.txttenkh.Enabled = False
        Me.btluu.Enabled = False
        Me.btsua.Enabled = False
        Me.btxoa.Enabled = False
        Me.btinhodon.Enabled = False
        Me.cbxtensp.Text = "       --- Chọn --- "
        Me.cbxtennv.Text = "       --- Chọn --- "
        Me.cbxdvt.Text = "    --- Chọn --- "
    End Sub
    Sub Mo()
        Me.cbxtensp.Enabled = True
        Me.txtsoluong.Enabled = True
        Me.cbxdvt.Enabled = True
        Me.txtdongia.Enabled = True
        Me.txtthanhtien.Enabled = True
        Me.btsua.Enabled = True
        Me.btxoa.Enabled = True
        Me.btluu.Enabled = True
        Me.btinhodon.Enabled = True

    End Sub
    Private Sub btinhodon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btinhodon.Click
        'R_HoaDon.MdiParent = Me
        'R_HoaDon.Show()
        R_HoaDon.ShowDialog()
    End Sub


    Private Sub dtg_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtg.SelectionChanged
        ' Me.cbxtensp.Text = dtg.CurrentRow.Cells(0).Value.ToString()
        ' Me.txtsoluong.Text = dtg.CurrentRow.Cells(2).Value.ToString()
        Me.cbxdvt.Text = dtg.CurrentRow.Cells(3).Value.ToString()
        Me.txtdongia.Text = dtg.CurrentRow.Cells(4).Value.ToString()
        Me.txtthanhtien.Text = dtg.CurrentRow.Cells(5).Value.ToString()
    End Sub

    Private Sub dtg_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtg.CurrentCellChanged

    End Sub

    Private Sub soluong_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles soluong.ValueChanged

        'Me.soluong.Value = Me.txtdongia.Text * Me.txtsoluong.Text

    End Sub
End Class